package com.yanfan.maintain.service.impl;

import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.iot.domain.Device;
import com.yanfan.iot.service.IDeviceService;
import com.yanfan.maintain.mapper.MaintenanceDeviceDao;
import com.yanfan.maintain.service.IMiniAppApiService;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class MiniAppApiServiceImpl implements IMiniAppApiService {

    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private MaintenanceDeviceDao maintenanceDeviceDao;

    @Override
    public Map<String, Object> findMaintailDeviceCount(Device device) {
        SysUser curruser = SecurityUtils.getLoginUser().getUser();

        // 查询设备数量
        Map<String, Object> result = new HashMap<>();
        long deviceCount = 0L;
        if (null == curruser.getDeptId()) {
            // 终端用户查询设备
            device.setTenantId(curruser.getUserId());
            deviceCount = maintenanceDeviceDao.countListTerminalUser(device);
        } else {
            if (null == device.getDeptId()) {
                device.setDeptId(curruser.getDeptId());
            }
            if (Objects.isNull(device.getTenantId())) {
                device.setTenantId(curruser.getUserId());
            }
            if (null == device.getShowChild()) {
                device.setShowChild(false);
            }
            deviceCount = maintenanceDeviceDao.countDevice(device);
        }
        result.put("deviceCount", deviceCount);

        // 查询正在维修的设备
        long underRepairCount = maintenanceDeviceDao.countUnderRepairDevice(device);
        result.put("underRepairCount", underRepairCount);

        // 查询未修复的设备
        long unrepairCount = maintenanceDeviceDao.countUnrepairDevice(device);
        result.put("unrepairCount", unrepairCount);


        return result;
    }

    @Override
    public Map<String, Object> findMaintailTodoCount() {
        Map<String, Object> result = new HashMap<>();
        Long submitted = maintenanceDeviceDao.selectMaintailTodoCountByStatus(1);
        result.put("submitted", submitted);
        Long approved = maintenanceDeviceDao.selectMaintailTodoCountByStatus(2);
        result.put("approved", approved);
        Long assigned = maintenanceDeviceDao.selectMaintailTodoCountByStatus(3);
        result.put("assigned", assigned);
        Long finished = maintenanceDeviceDao.selectMaintailTodoCountByStatus(5);
        result.put("finished", finished);
        return result;
    }
}
